package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 星岛环球网解析
 * @author Persh
 *
 */
public class StnnNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://.*.stnn.cc/.*/[0-9]{6}/t[0-9]{8}_[0-9]*.html",
		"http://.*.stnn.cc/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.html"
		,"http://.*.stnn.cc/.*/[0-9]{4}/[0-9]{4}/[0-9].*.shtml"};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("div.font_title>h1").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("span#c_title").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("h1.article-title").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("h1.h1").text();
					}
					if(title==null || title.trim().length()==0){
					}
				}
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div#content_zoom"));
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.article-content"));
				if(content==null || content.trim().length()==0){
					content=HtmlCleaner.getContentHtml(url,doc.select("header.picture-header"));
				}
			}
			
			
			String tempdate=doc.select("div.l_box08").text();
			if(tempdate==null || tempdate.trim().length()==0){
				tempdate=doc.select("span.float_r").text();
				if(tempdate==null || tempdate.trim().length()==0){
					tempdate=doc.select("span.float_l").text();
					if(tempdate==null || tempdate.trim().length()==0){
						tempdate=doc.select("span.post-time").text();
					}
				}
			}
			date=DateUtils.matchDate(tempdate);
			if(date==null){
				date=DateUtils.matchDate(url);
			}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);	
		
			String author=doc.select("span.source").text();
			
			news.setAuthor(author);
		return news;
	}
	
	public static void main(String[] args) {
		StnnNewsAnalyse analyse=new StnnNewsAnalyse();
		String url="http://news.stnn.cc/picture/2014/0328/81140.shtml";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
